package com.ndpmedia.fb.pmd.configration.web.resource;

import com.alibaba.fastjson.JSON;
import com.ndpmedia.fb.pmd.configration.common.Page;
import com.ndpmedia.fb.pmd.configration.common.Result;
import com.ndpmedia.fb.pmd.configration.service.PropertiesService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@Path("properties")
@Component
public class PropertiesResource {

    /**
     * 日志对象
     */
    private static final Logger LOG = LoggerFactory.getLogger(PropertiesResource.class);
    @Autowired
    PropertiesService propertiesService;

    @GET
    @Path("list")
    @Produces(MediaType.APPLICATION_JSON)
    public String getList(@QueryParam("appid") String appid, @QueryParam("configid") String configid) {
        return list(appid, configid);
    }


    @POST
    @Path("list")
    @Produces(MediaType.APPLICATION_JSON)
    public String postList(@FormParam("appid") String appid, @FormParam("configid") String configid) {
        return list(appid, configid);
    }

    private String list(String appid, String configid) {
        Result result = null;
        try {
            List list = propertiesService.list(appid, configid);
            Page page = new Page(1, 1, list == null ? 0 : list.size(), 1);
            result = new Result(1, list, page, "");
        } catch (Exception e) {
            result = new Result(0, null, e.getMessage());
        }
        return JSON.toJSONString(result);
    }

    @GET
    @Path("add")
    @Produces(MediaType.APPLICATION_JSON)
    public String getAdd(@QueryParam("appid") String appid, @QueryParam("configid") String configid,
                         @QueryParam("key") String key, @QueryParam("value") String value,@QueryParam("description") String description) {
        return add(appid, configid, key, value,description);
    }


    @POST
    @Path("add")
    @Produces(MediaType.APPLICATION_JSON)
    public String postAdd(@FormParam("appid") String appid, @FormParam("configid") String configid, @FormParam("key") String key,
                          @FormParam("value") String value,@FormParam("description") String description) throws  Exception{
        return add(appid, configid, key, value, description);
    }

    private String add(String appid, String configid, String key, String value,String description) {
        Result result = null;
        try {
            result = new Result(1, propertiesService.add(appid, configid, key, value,description), "");
        } catch (Exception e) {
            result = new Result(0, null, e.getMessage());
        }
        return JSON.toJSONString(result);
    }

    @GET
    @Path("delete")
    @Produces(MediaType.APPLICATION_JSON)
    public String getDelete(@QueryParam("propertiesid") String propertiesid) {
        return delete(propertiesid);
    }


    @POST
    @Path("delete")
    @Produces(MediaType.APPLICATION_JSON)
    public String postDelete(@FormParam("propertiesid") String propertiesid) {
        return delete(propertiesid);
    }

    private String delete(String propertiesid) {
        Result result = null;
        try {
            propertiesService.delete(propertiesid);
            result = new Result(1, new Date().getTime() + "", "");
        } catch (Exception e) {
            result = new Result(0, null, e.getMessage());
        }
        return JSON.toJSONString(result);
    }

    @GET
    @Path("update")
    @Produces(MediaType.APPLICATION_JSON)
    public String getUpdate(@QueryParam("propertiesid") String propertiesid, @QueryParam("key") String key,
                            @QueryParam("value") String value,@QueryParam("description") String description) {
        return update(propertiesid, key, value,description);
    }


    @POST
    @Path("update")
    @Produces(MediaType.APPLICATION_JSON)
    public String postUpdate(@FormParam("propertiesid") String propertiesid, @FormParam("key") String key,
                             @FormParam("value") String value, @FormParam("description") String description) {
        return update(propertiesid, key, value,description);
    }

    private String update(String propertiesid, String key, String value,String description) {
        Result result = null;
        try {
            propertiesService.update(propertiesid, key, value,description);
            result = new Result(1, new Date().getTime() + "", "");
        } catch (Exception e) {
            result = new Result(0, null, e.getMessage());
        }
        return JSON.toJSONString(result);
    }

    @GET
    @Path("push")
    @Produces(MediaType.APPLICATION_JSON)
    public String getPush(@QueryParam("appid") String appid, @QueryParam("configid") String configid, @QueryParam("propertiesid") String propertiesid) {
        return push(appid, configid, propertiesid);
    }


    @POST
    @Path("push")
    @Produces(MediaType.APPLICATION_JSON)
    public String postPush(@FormParam("appid") String appid, @FormParam("configid") String configid, @FormParam("propertiesid") String propertiesid) {
        return push(appid, configid, propertiesid);
    }


    private String push(String appid, String configid, String propertiesid) {
        Result result = null;
        try {
            propertiesService.push(appid, configid, propertiesid);
            result = new Result(1, new Date().getTime() + "", "");
        } catch (Exception e) {
            result = new Result(0, null, e.getMessage());
        }
        return JSON.toJSONString(result);
    }
}
